﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ivi.Visa;
using Ivi.Visa.FormattedIO;
using Microsoft.Win32;
using NationalInstruments.Visa;
using NLog;


namespace CSharpTest_OSC
{
    internal class Program
    {
        static void Main(string[] args)
        {

            //创建一个配置文件对象
            var config = new NLog.Config.LoggingConfiguration();
            //创建日志写入目的地
            var logfile = new NLog.Targets.FileTarget("logfile") { FileName = $"logs/{DateTime.Now.ToString("yyyy-MM-dd")}.txt" };
            //添加日志路由规则
            config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile);
            //配置文件生效
            LogManager.Configuration = config;
            //创建日志记录对象方式1
            Logger Logger = LogManager.GetCurrentClassLogger();
            Logger.Debug("我打出了Nlog日志！");
            Logger.Fatal("Fatal Record!");

            ResourceManager rmSession = new ResourceManager();
            var resources = rmSession.Find("?*");
            var tmp = rmSession.Find("USB0::0xF4EC::0x100C::SDS2HBAD6R0410::INSTR");
            var tmp1 = rmSession.Find("USB?*");
            //foreach (var resource in resources)
            //{
            //   Console.WriteLine(resource);
            //}


            //var resources = rmSession.Find("(ASRL|GPIB|TCPIP|USB)?*");

            var mbSession = (MessageBasedSession)rmSession.Open("USB0::0xF4EC::0x100C::SDS2HBAD6R0410::INSTR");
            mbSession.TimeoutMilliseconds = 5000;
            string quaryStr = "*IDN?";
            char LF = (char)0x0A;
            quaryStr = quaryStr + LF;

            //mbSession.RawIO.Write(InsertCommonEscapeSequences(quaryStr));
            mbSession.RawIO.Write(quaryStr);
            string oscInfo = mbSession.RawIO.ReadString();
            Console.WriteLine(oscInfo);

            Digital_Oscilloscope dosc = null;
            if (oscInfo.Contains("Siglent"))
            {
                dosc = new Siglent_Digital_Oscilloscope();
            }
            else
            {
                return;
            }

            dosc.snapShot(mbSession);

            var sdosc  = dosc as Siglent_Digital_Oscilloscope;
            sdosc.readWaveform(mbSession);


            mbSession.RawIO.Write("TRIG:Mode AUTO");
            // TRIG:Mode AUTO  TRIG:Mode NORM  TRIG:Mode SING

            mbSession.Dispose();
        }

        private static string InsertCommonEscapeSequences(string s)
        {
            return s.Replace("\n", "\\n").Replace("\r", "\\r");
        }
    }
}
